'use strict'

let sceneMgr = require('../core/sceneMgr')
let playerDao = require('../dao').PlayerDao
let tradeDao = require('../dao').TradeDao
let templateConfig = require('../../config/templateConfig')
let ItemComponent = require('../component/itemComponent')
let attackHandle = require('../core/attackHandle')
let adventure = require('../core/adventure')
let fieldBossComponent = require('../component/fieldBossComponent')
let arenaHandle = require('../component/arenaComponent')
let route = require('./route')
let pushMsg = require('./pushMsg')
let playerSessionMgr = require('../core/playerSessionMgr')
let mailDao = require('../dao').MailDao
let PlayerModel = require('../dao/model').Player
let trialHandle = require('../component/trialComponent')
let guardComponent = require('../component/guardComponent')
let utils = require('../utils/utils')
let gateHandle = require('./gateHandle')

//前往秘境入口
route.set('entrance', function(player, msg, next) {
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 1) {
    next(1)
    return
  }
  player.basicInfo.mapIndex = 2
  next({
    error: 0,
    basicInfo: player.basicInfo
  })
})

//开启秘境
route.set('createSecret', function(player, msg, next) {
  let secretMapId = msg.secretMapId
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 2) {
    next(1)
    return
  }

  let secretMapConfig = templateConfig.secretMap[secretMapId]
  if (!secretMapConfig) {
    next(2)
    return
  }
  player.basicInfo.secretMapId = secretMapId
  adventure.createSecret(player, secretMapConfig)
  player.basicInfo.lattice = 0
  next({
    error: 0,
    secretMap: player.basicInfo.secretMap
  })
})

//进入秘境
route.set('entranceSecret', function(player, msg, next) {
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 2) {
    next(1)
    return
  }
  let secretMap = player.basicInfo.secretMap
  if (!secretMap) {
    next(2)
    return
  }
  player.basicInfo.mapIndex = 3
  player.basicInfo.lattice = 1
  player.basicInfo.state = 1
  let needNum = 0
  let cureBar = player.cureBar
  if (cureBar && cureBar.stack < 3) {
    needNum = 3 - cureBar.stack
    let num = player.bag.getTotalNumByItemId(240101)
    if (num > 0 && num <= needNum) {
      cureBar.stack += num
      player.bag.removeItemByItemId(240101, num)
    } else if (num > needNum) {
      cureBar.stack += needNum
      player.bag.removeItemByItemId(240101, needNum)
    }
  } else if (!cureBar) {
    needNum = 3
    let num = player.bag.getTotalNumByItemId(240101)
    if (num > 0 && num <= 3) {
      let itemComponent = templateConfig.total[240101].getItemComponent(num)
      player.cureBar = itemComponent
      player.bag.removeItemByItemId(240101, num)
    } else if (num > 3) {
      let itemComponent = templateConfig.total[240101].getItemComponent(3)
      player.cureBar = itemComponent
      player.bag.removeItemByItemId(240101, 3)
    }
  }

  next({
    error: 0,
    basicInfo: player.basicInfo
  })
})

//进入秘境守护之地
route.set('deepSecret', function(player, msg, next) {
  if (player.basicInfo.lvl < 3) {
    next(5)
    return
  }
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 3) {
    next(1)
    return
  }

  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType != 2) { //是否是岗哨
    next(2)
    return
  }
  player.basicInfo.mapIndex = 4
  player.basicInfo.sceneId = 260002
  gateHandle.callRemoteFunc('enterScene', {
    sceneId: 260002,
    pId: player._id
  })
  next({
    error: 0
  })
})

//进入深处岗哨
route.set('enterDeepPost', function(player, msg, next) {
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 3) {
    next(1)
    return
  }

  let lattice = player.basicInfo.lattice + 1
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 2) { //判断深处岗哨
    next(2)
    return
  }
  player.basicInfo.lattice += 1
  player.basicInfo.stamina = 10
  next({
    error: 0,
    lattice: player.basicInfo.lattice
  })
})

//进入魔窟
route.set('enterDevilCave', function(player, msg, next) {
  if (player.basicInfo.lvl < 4) {
    next(5)
    return
  }
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 4) {
    next(1)
    return
  }
  player.basicInfo.sceneId = 260001
  gateHandle.callRemoteFunc('enterScene', {
    sceneId: 260001,
    pId: player._id
  })

  player.basicInfo.mapIndex = 5
  next({
    error: 0,
    basicInfo: player.basicInfo
  })
})

//魔窟击杀怪物(包括boss)
route.set('attackDevilCave', function(player, msg, next) {
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 5) {
    next(1)
    return
  }

  if (player.fightAttri.hp <= 0) {
    next(2)
    return
  }

  let devilCaveIndex = player.basicInfo.devilCaveIndex
  let devilCaveId = templateConfig.secretMap[player.basicInfo.secretMapId].devilCaveId
  let devilCave = templateConfig.devilCave[devilCaveId]
  if (devilCaveIndex >= devilCave.length) {
    next(3)
    return
  }
  let monsterId = devilCave.monsters[devilCaveIndex]
  let fightMsg = attackHandle.attackDevilCaveMonster(player, monsterId)
  if (fightMsg.result === 1) {
    player.basicInfo.devilCaveIndex += 1
  } else {
    //战斗失败,离开魔窟
    player.basicInfo.mapIndex = 4
      /*    let secretMapId = player.basicInfo.secretMapId
          let secretMapConfig = templateConfig.secretMap[secretMapId]
          let guardSceneId = secretMapConfig.guardSceneId
          player.basicInfo.sceneId = guardSceneId*/
    player.basicInfo.sceneId = 260002
    gateHandle.callRemoteFunc('enterScene', {
      sceneId: 260002,
      pId: player._id
    })
  }
  next({
    error: 0,
    fightMsg: fightMsg,
    basicInfo: player.basicInfo
  })
})

//离开魔窟
route.set('exitDevilCave', function(player, msg, next) {
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 5) {
    next(1)
    return
  }
  player.basicInfo.mapIndex = 4
  player.basicInfo.sceneId = 260002
  gateHandle.callRemoteFunc('enterScene', {
    sceneId: 260002,
    pId: player._id
  })
  next({
    error: 0
  })
})

//获取秘境守护之地信息
route.set('getDeepSecret', function(player, msg, next) {
  let mapIndex = player.basicInfo.mapIndex
  if (mapIndex !== 4) {
    next(1)
    return
  }

  let secretMapId = player.basicInfo.secretMapId
  let secretMapConfig = templateConfig.secretMap[secretMapId]
  let guardSceneId = secretMapConfig.guardSceneId
  sceneMgr.getSceneSerById(guardSceneId).addClient(player)
  let info = guardComponent.getFieldBossInfo(guardSceneId, player._id)
  next({
    error: 0,
    fieldBoss: info.fieldBossInfo,
    myDamage: info.myDamage
  })
})

//冒险
route.set('adventure', function(player, msg, next) {
  if (player.basicInfo.stamina === 0) {
    adventure.returnCamp(player)
    next({
      error: 0,
      state: 1,
      basicInfo: player.basicInfo
    })
    return
  }
  //判断人物的状态
  if (player.basicInfo.stamina === 0 || player.fightAttri.hp <= 0) {
    next(1)
    return
  }

  if (player.basicInfo.mapIndex !== 3) {
    next(2)
    return
  }

  let lattice = player.basicInfo.lattice
  if (lattice >= player.basicInfo.secretMap.length - 2) {
    next(3)
    return
  }
  player.basicInfo.stamina -= 1
  if (player.basicInfo.stamina === 0) {
    adventure.returnCamp(player)
    next({
      error: 0,
      state: 1,
      basicInfo: player.basicInfo
    })
    return
  }

  player.basicInfo.state = 1
  player.basicInfo.lattice += 1
  let adventureInfo = adventure.latticeAction(player)
  next({
    error: 0,
    adventureInfo: adventureInfo,
    basicInfo: player.basicInfo
  })
})

//跑尸
route.set('corpse', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }
  //判断人物的状态
  if (player.fightAttri.hp > 0) {
    next(2)
    return
  }

  let num = 0
  let length = player.adventureBag.length
  if (length > 1) {
    num = Math.floor(Math.random() * length)
    for (let i = 0; i < num; i++) {
      let len = player.adventureBag.length
      let j = Math.floor(Math.random() * len)
      player.adventureBag.splice(j, 1)
    }
  }
  player.basicInfo.moneyBuff = 0
  player.basicInfo.luckyBuff = 0
  player.basicInfo.maxHpBuff = 0
  player.calcAttri()
  player.fightAttri.hp = Math.floor(player.fightAttri.maxHp / 2)

  next({
    error: 0,
    basicInfo: player.basicInfo,
    num: num
  })
})

//泡温泉
route.set('hotSpring', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }
  if (player.basicInfo.state != 1 || player.basicInfo.stamina === 2) {
    next(2)
    return
  }


  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 4) { //判断是否是温泉
    next(4)
    return
  }
  player.basicInfo.stamina -= 1
  player.fightAttri.hp = player.fightAttri.maxHp
  player.basicInfo.state = 2
  next({
    error: 0,
    basicInfo: player.basicInfo
  })
})

//采矿
route.set('mining', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }
  //判断人物的状态
  if (player.basicInfo.state !== 1 || player.basicInfo.stamina === 2) {
    next(2)
    return
  }

  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 6) { //判断是否是矿井
    next(4)
    return
  }
  player.basicInfo.stamina -= 1
  player.basicInfo.state = 2
  let fightMsg = null
  if (Math.floor(Math.random() * 100) < latticeConfig.eventRate) { //判断遇到怪物的概率
    let monsterNames = templateConfig.other[290034]['mining']
    let monsterName = utils.getOneFromArray(monsterNames)
    fightMsg = attackHandle.attackMonster(player, monsterName)
    next({
      error: 0,
      fightMsg: fightMsg,
      basicInfo: player.basicInfo
    })
    return
  }
  let secretMapId = player.basicInfo.secretMapId
  let mapConfig = templateConfig.secretMap[secretMapId]
  let itemsRate = latticeConfig.itemsRate.slice(0)
  let index = utils.getIndexByLucky(player.fightAttri.lucky, mapConfig.luckyRate, itemsRate)
  let items = latticeConfig.items[index]
  let length = items.length
  let i = Math.floor(Math.random() * length)
  let itemId = items[i]
  let num = Math.floor(Math.random() * 2 + 1)
  let itemComponent = templateConfig.prop[itemId].getItemComponent(num)
  player.adventureBag.push(itemComponent) //添加物品
  next({
    error: 0,
    itemId: itemId,
    num: num,
    fightMsg: fightMsg,
    basicInfo: player.basicInfo
  })
})

//采集
route.set('collection', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }

  //判断人物的状态
  if (player.basicInfo.state !== 1 || player.basicInfo.stamina === 2) {
    next(2)
    return
  }
  player.basicInfo.stamina -= 1

  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 5) { //判断是否是采集
    next(3)
    return
  }
  player.basicInfo.state = 2
  let fightMsg = null
  if (Math.floor(Math.random() * 100) < latticeConfig.eventRate) { //判断遇到怪物的概率
    let monsterNames = templateConfig.other[290034]['collection']
    let monsterName = utils.getOneFromArray(monsterNames)
    fightMsg = attackHandle.attackMonster(player, monsterName)
    next({
      error: 0,
      fightMsg: fightMsg,
      basicInfo: player.basicInfo
    })
    return

  }
  let secretMapId = player.basicInfo.secretMapId
  let mapConfig = templateConfig.secretMap[secretMapId]
  let itemsRate = latticeConfig.itemsRate.slice(0)
  let index = utils.getIndexByLucky(player.fightAttri.lucky, mapConfig.luckyRate, itemsRate)
  let items = latticeConfig.items[index]
  let i = Math.floor(Math.random() * items.length)
  let itemId = items[i] //物品id
  let itemComponent = templateConfig.prop[itemId].getItemComponent()
  let num = Math.floor(Math.random() * 2 + 1)
  itemComponent.stack = num
  player.adventureBag.push(itemComponent) //添加物品

  next({
    error: 0,
    itemId: itemId,
    num: num,
    fightMsg: fightMsg,
    basicInfo: player.basicInfo
  })
})

//钓鱼
route.set('fishing', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }
  //判断人物的状态
  if (player.basicInfo.state !== 1 || player.basicInfo.stamina === 2) {
    next(2)
    return
  }



  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 7) { //判断是否是钓鱼
    next(4)
    return
  }
  player.basicInfo.stamina -= 1
  player.basicInfo.state = 2
  let fightMsg = null
  if (Math.floor(Math.random() * 100) < latticeConfig.eventRate) { //判断遇到怪物的概率
    let monsterNames = templateConfig.other[290034]['fishing']
    let monsterName = utils.getOneFromArray(monsterNames)
    fightMsg = attackHandle.attackMonster(player, monsterName)
    next({
      error: 0,
      fightMsg: fightMsg,
      basicInfo: player.basicInfo
    })
    return
  }

  let secretMapId = player.basicInfo.secretMapId
  let mapConfig = templateConfig.secretMap[secretMapId]
  let itemsRate = latticeConfig.itemsRate.slice(0)
  let index = utils.getIndexByLucky(player.fightAttri.lucky, mapConfig.luckyRate, itemsRate)
  let items = latticeConfig.items[index]
  let i = Math.floor(Math.random() * items.length)
  let itemId = items[i] //物品id
  let itemComponent = templateConfig.prop[itemId].getItemComponent()
  let num = Math.floor(Math.random() * 2 + 1)
  itemComponent.stack = num
  player.adventureBag.push(itemComponent) //添加物品
  next({
    error: 0,
    itemId: itemId,
    num: num,
    fightMsg: fightMsg,
    basicInfo: player.basicInfo
  })
})

//挑战副本
route.set('attackFam', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }
  //判断人物的状态
  if (player.basicInfo.state !== 1 || player.basicInfo.stamina === 2) {
    next(2)
    return
  }

  player.basicInfo.stamina -= 1

  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 12) { // 判断是否是副本
    next(3)
    return
  }
  //攻击该怪物
  let battleMsg = attackHandle.attackCaveMonster(player, latticeConfig.monsterId)
  player.basicInfo.state = 2
  next({
    error: 0,
    battleMsg: battleMsg,
    basicInfo: player.basicInfo
  })
})

//护送物资
route.set('transport', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }

  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 2) { // 判断是否是岗哨
    next(3)
    return
  }

  let secretMap = player.basicInfo.secretMap
  let transportInfos = []
  transportInfos[lattice] = null
  let flag = 1 //1成功0失败
  for (let i = lattice; i >= 1; i--) {
    let latticeType = secretMap[i].latticeType
    let res = adventure.transportAction(player, i)
    if (res !== 0) {
      transportInfos[i] = res
      if (player.fightAttri.hp <= 0) {
        transportInfos[i]['result'] = -1
        flag = 0
        break
      }
    }
  }
  let getProp = 0
  if (flag === 1) {
    //护送成功
    let transportItem = latticeConfig.transportItem
    let index = Math.floor(Math.random() * transportItem.length)
    let itemId = transportItem[index]
    let itemComponent = templateConfig.total[itemId].getItemComponent()
    itemComponent.stack = 1
    getProp = transportItem[index]
    player.adventureBag.push(itemComponent) //添加物品到冒险背包
      //回到营地,恢复生命和体力
    player.basicInfo.moneyBuff = 0
    player.basicInfo.luckyBuff = 0
    player.basicInfo.maxHpBuff = 0
    player.calcAttri()
    player.fightAttri.hp = player.fightAttri.maxHp
    player.basicInfo.stamina = player.fightAttri.maxStamina
    player.basicInfo.lattice = 0
    player.basicInfo.secretMap = 0
    player.basicInfo.mapIndex = 1
  } else {
    //护送失败
    //回到营地,恢复生命和体力
    player.calcAttri()
    player.fightAttri.hp = player.fightAttri.maxHp
    player.basicInfo.stamina = player.fightAttri.maxStamina
    player.basicInfo.lattice = 0
    player.basicInfo.secretMap = 0
    player.basicInfo.mapIndex = 1
  }
  next({
    error: 0,
    transportInfos: transportInfos,
    getProp: getProp,
    basicInfo: player.basicInfo
  })
})


//返回营地
route.set('returnCamp', function(player, msg, next) {
  //回到营地,恢复生命和体力
  player.basicInfo.luckyBuff = 0
  player.basicInfo.moneyBuff = 0
  player.basicInfo.maxHpBuff = 0
  player.calcAttri()
  player.fightAttri.hp = player.fightAttri.maxHp
  player.basicInfo.stamina = player.fightAttri.maxStamina
  player.basicInfo.lattice = 0
  player.basicInfo.secretMap = 0
  player.basicInfo.secretMapId = 0
  player.basicInfo.mapIndex = 1
  next({
    error: 0,
    basicInfo: player.basicInfo
  })
})


//触发事件
route.set('triggerEvent', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }

  //判断人物的状态
  if (player.basicInfo.state !== 1 || player.basicInfo.stamina === 0) {
    next(2)
    return
  }

  //判断是否是类型3
  let lattice = player.basicInfo.lattice
  let latticeInfo = player.basicInfo.secretMap[lattice]
  let latticeId = latticeInfo.latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 3) {
    next(3)
    return
  }
  let eventType = latticeInfo['eventType']
  if (!eventType || eventType < 2) {
    next(3)
    return
  }
  let eventPoolId = latticeConfig.eventPool
  let eventPool = templateConfig.total[eventPoolId]
  let res = adventure.triggerEvent(player, eventType, eventPool)
  player.basicInfo.state = 2
  next(res)
})


//墓地祈祷
route.set('pray', function(player, msg, next) {
  if (player.basicInfo.mapIndex !== 3) {
    next(1)
    return
  }

  //判断人物的状态
  if (player.basicInfo.state !== 1 || player.basicInfo.stamina === 2) {
    next(2)
    return
  }

  player.basicInfo.stamina -= 1

  //判断是否是墓地
  let lattice = player.basicInfo.lattice
  let latticeId = player.basicInfo.secretMap[lattice].latticeId
  let latticeConfig = templateConfig.lattice[latticeId]
  if (latticeConfig.latticeType !== 10) { // 判断是否是墓地
    next(3)
    return
  }
  player.basicInfo.state = 2
  let buffs = latticeConfig.buffs
  let index = Math.floor(Math.random() * buffs.length)
  let buffValue = buffs[index]
  if (index === 0) { //幸运buff
    player.basicInfo.luckyBuff += buffValue
  } else if (index === 1) { //金钱buff
    player.basicInfo.moneyBuff += buffValue
  } else if (index === 2) { //生命上限buff
    player.basicInfo.maxHpBuff += buffValue
  }
  player.calcAttri()
  next({
    error: 0,
    index: index,
    buffValue: buffValue,
    basicInfo: player.basicInfo
  })
})
